<template>
  <el-container>
    <el-header>
      <h2>新建任务</h2>
    </el-header>
    <el-main>
      <el-form :model="task" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="task.name" placeholder="请输入任务名称"></el-input>
        </el-form-item>

        <el-form-item label="结束时间" prop="end_time">
          <el-date-picker
              v-model="task.end_time"
              type="datetime"
              placeholder="Select date and time"
              style="width: 100%"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="任务描述" prop="_describe">
          <el-input type="textarea" v-model="task._describe" placeholder="请输入任务描述"></el-input>
        </el-form-item>
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="task.status">
            <el-option
                v-for="status in statuses"
                :key="status.value"
                :label="status.label"
                :value="status.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 其他需要的字段... -->

        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import {  reactive } from 'vue';
import axios from 'axios';


export default {
  setup() {
    const task = reactive({
      name: '',
      _describe: '',
      end_time: '',
      status: '',
    });
    const rules = {
      name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
      _describe: [{ required: true, message: '请输入任务描述', trigger: 'blur' }],
      end_time: [{ required: true, message: '时间', trigger: 'blur' }],
      status: [{ required: true, message: '请选择任务状态', trigger: 'change' }],
    };
    const statuses = [
      { value: 'no', label: '进行中' },
      { value: 'yes', label: '已完成' },
    ];

    const onSubmit = async () => {
      try {
        const response = await axios.post('/api/create_job', task);

        if (response.data.code==200) {
          alert('任务创建成功');
          console.log("====1"+response.data)

          console.log("====2")
          resetForm();

        } else {
          alert('任务创建失败，请稍后重试');
        }
      } catch (error) {
        console.error(error);
        alert('任务创建失败，请稍后重试9');
      }
    };

    const resetForm = () => {
      task.name = '';
      task._describe = '';
      task.end_time = '';
      task.status = '';
    };

    return {
      task,
      rules,
      statuses,
      onSubmit,
      resetForm,
    };
  },
};
</script>
